package com.rockontrol.ele.service.model.dbtransaction.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.rksp.svc.BaseSvc;
import com.rksp.util.DatabaseUtil;
import com.rksp.util.DateUtil;
import com.rksp.util.StringUtil;
import com.rockontrol.ele.pojo.Ma出入库单;
import com.rockontrol.ele.pojo.Ma出入库明细;
import com.rockontrol.ele.pojo.Ma回收单;
import com.rockontrol.ele.pojo.Ma帐套;
import com.rockontrol.ele.pojo.Ma报废单;
import com.rockontrol.ele.pojo.Ma维修计划;
import com.rockontrol.ele.pojo.Ma维修计划明细;
import com.rockontrol.ele.pojo.Ma维修费用明细;
import com.rockontrol.ele.pojo.Ma设备;
import com.rockontrol.ele.pojo.Ma设备报废明细;
import com.rockontrol.ele.service.model.dbtransaction.MaEquipmentTrans;
import com.rockontrol.ele.vo.conditionDetaiVo;

public class MaEquipmentTransImpl extends BaseSvc implements MaEquipmentTrans {

    public String serviceCreateEqScrap(Ma报废单 eqScrap){
        try{
            service.addObj(eqScrap);
            List<Ma设备报废明细> list = new ArrayList<Ma设备报废明细>();
            list = eqScrap.getEqBfList();
            if(list != null && list.size() > 0){
                for(int i = 0; i < list.size(); i++){
                    list.get(i).set单据id(eqScrap.getOid());
                    service.addObj(list.get(i));
                }
            }
            return eqScrap.getOid();
        }catch(Exception e){
            e.printStackTrace();
            throw new RuntimeException();
           
        }
    }

    public Boolean serviceDeleteEqScrap(String id){
        Boolean flag = false;
        try {
             service.execSQL(" delete from MA_报废单   where OID='" + id + "' ");
             service.execSQL(" delete from MA_设备报废明细   where 单据ID='" + id + "'");
             flag = true;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
        return flag;
    }

    public Boolean serviceUpdateEqScrap(Ma报废单 eqScrap){
        Boolean flag = false;
        try {
            service.modifyObj(eqScrap);
            List<Ma设备报废明细> list=eqScrap.getEqBfList();
            service.execHQL("delete from Ma设备报废明细 a  where  a.单据id ='" + eqScrap.getOid() + "' ");
            if(list!=null&&list.size()>0){
                for(Ma设备报废明细 detail:list){
                    detail.set单据id(eqScrap.getOid());
                    service.addObj(detail);
                    if(eqScrap.isUpdateInventory()){
                        service.execHQL("update Ma设备 a set a.设备状态='报废' where a.oid='"+detail.get设备id()+"'");
                        String dataBaseType = DatabaseUtil.getDataBaseType(dataSource);
                        if("SQLSERVER".equals(dataBaseType)){
                                //数据库为sqlserver时执行的操作
                            service.execHQL("update Ma设备台帐 a set a.状态='未管',a.调出时间=getdate() where a.设备id = '"+detail.get设备id()+"' and a.状态='在管'");
                        }else if("ORACLE".equals(dataBaseType)){
                                //数据库为oracle时执行的操作
                            service.execHQL("update Ma设备台帐 a set a.状态='未管',a.调出时间=sysdate where a.设备id = '"+detail.get设备id()+"' and a.状态='在管'");
                        }
                    }
                }
            }
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
        return flag;
    }

    public String serviceCreateEquipment(Ma设备 Eq_){
           service.addObj(Eq_);
        return Eq_.getOid();
    }
    public String serviceCreateEqRecover(Ma回收单 er){
        return null;
    }
    public String serviceCreateMaOverhaulPlan(Ma维修计划 eop){
        try{
            service.addObj(eop);
            List<Ma维修计划明细> list=eop.getDetailList();
            if(list != null && list.size() > 0){
                for(int i = 0; i < list.size(); i++){
                    list.get(i).set维修计划id(eop.getOid());
                    service.addObj(list.get(i));
                }
            }
        }catch(Exception e){
            e.printStackTrace();
            throw new RuntimeException();
        }
        return eop.getOid();
    }

    public Boolean serviceUpdateMaOverhaulPlan(Ma维修计划 eop){
        Boolean flag = false;
        try {
            service.modifyObj(eop);
            List<Ma维修计划明细> list=eop.getDetailList();
            service.execSQL(" delete from MA_维修计划明细  where  维修计划ID ='" + eop.getOid() + "' ");
            if(list!=null&&list.size()>0){
                for(int i=0;i<list.size();i++){
                    list.get(i).set维修计划id(eop.getOid());
                    service.addObj(list.get(i));
                }
            }
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
        return flag;
    }

    public Boolean serviceDeleteMaOverhaulPlan(String id){
        Boolean flag = false;
        try {
             service.execSQL(" delete from MA_维修计划   where OID='" + id + "' ");
             service.execSQL(" delete from MA_维修计划明细   where 维修计划ID='" + id + "'");
             flag = true;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
        return flag;
    }

    public String serviceCreateMaOverhaul(Ma维修计划明细 eo_) {
        service.addObj(eo_);
        if(eo_.getFyList()!=null){
            for(Ma维修费用明细 fy:eo_.getFyList()){
                fy.set维修计划明细id(eo_.getOid());
                service.addObj(fy);
            }
        }
        return eo_.getOid();
    }

    public Boolean serviceUpdateMaOverhaul(Ma维修计划明细 eo_) {
        service.modifyObj(eo_);
        service.execHQL("delete from Ma维修费用明细 a where a.维修计划明细id='"+eo_.getOid()+"'");
        if(eo_.getFyList()!=null){
            for(Ma维修费用明细 fy:eo_.getFyList()){
                fy.set维修计划明细id(eo_.getOid());
                service.addObj(fy);
            }
        }
        return true;
    }

    public Boolean serviceDeleteMaOverhaul(String id_) {
        service.removeObj(Ma维修计划明细.class,id_);
        service.execHQL("delete from Ma维修费用明细 a where a.维修计划明细id='"+id_+"'");
        return true;
    }

    public Boolean serviceAssignMauipment(String MaID, String receiveID) {
        if (StringUtil.isEmpty(receiveID))
            return false;
        String[] eqids = receiveID.split(",");
        
        Ma出入库明细 detail=(Ma出入库明细) service.findObj(Ma出入库明细.class, MaID);
        Ma出入库单 out=(Ma出入库单) service.findObj(Ma出入库单.class, detail.get单据id());
        
        service.execHQL("update Ma物资库存 a set a.库存量=a.库存量-"+detail.get实际数量()+" where a.帐套id='"+out.get出仓库id()+"' and a.物资分类id='"+detail.get物资分类id()+"' and a.规格型号id='"+detail.get规格型号id()+"'");
        
        Ma帐套 in=getTheMaterialAccount(out.get入仓库id(), out.get类型分类名());
        for(String eqid:eqids){
            Ma设备 ma设备=(Ma设备) service.findObj(Ma设备.class, receiveID);
            ma设备.set设备状态("在用");
            
        }
        return true;
    }
    private Ma帐套 getTheMaterialAccount (String id_,String type){
        if(StringUtil.isEmpty(id_)||StringUtil.isEmpty(type))
            return null;
        List<Ma帐套> list= service.findObjsHQL("from Ma帐套 a where a.单位id='"+id_+"' and a.类型分类名='"+type+"'");
        return list.isEmpty()?null:list.get(0);
    }
    
}
